//
//  MyController.m
//  SimpleHappy
//
//  Created by lanou on 15/11/23.
//  Copyright © 2015年 RockyFung. All rights reserved.
//

#import "MyController.h"
#import "Define.h"
#import "SHCollectionHead.h"
#import "SHMyCollectionViewCell.h"
#import "SHLoginView.h"
#import "SHSettingView.h"
#import "ListViewView.h"
#import "SHCoreData.h"
#import "SHMyLoginModel.h"
#import <BmobSDK/Bmob.h>
#import "MyHomeController.h"

@interface MyController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) NSString *userName;

@property (nonatomic, strong) UICollectionView *collectionView;

@property (nonatomic, strong) NSArray *labelArray;

@end

@implementation MyController

- (void)viewWillAppear:(BOOL)animated
{
    [self.collectionView reloadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.itemSize = CGSizeMake(KScreenWidth / 3.5, KScreenWidth / 3.5);
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight) collectionViewLayout:layout];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    self.collectionView.showsVerticalScrollIndicator = NO;

    
    // 注册表头、cell
    [self.collectionView registerClass:[SHCollectionHead class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headView"];
    [self.collectionView registerClass:[SHMyCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    [self.view addSubview:_collectionView];
    
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 2;
}

#pragma mark - 返回cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    SHMyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    self.labelArray = @[ @"心愿单", @"设置"];// @"优惠卷", @"邀请有礼", @"预订管理", @"贵宾卡", @"消息", @"意见反馈",
    cell.SHLabel.text = self.labelArray[indexPath.item];
    
    NSArray *imageArray = @[ @"XY",@"SZ"];//@"YH", @"YQ", @"YD", @"GB", @"XX", @"FK", 
    cell.SHImage.image = [UIImage imageNamed:imageArray[indexPath.item]];

    return cell;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, 10, 10, 10);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}

#pragma mark - 返回表头
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    SHCollectionHead *headView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headView" forIndexPath:indexPath];
    
    // 获取本地用户信息
    BmobUser *user = [BmobUser getCurrentUser];
    
    // 判断是否登录
    if (user) {
        _userName = user.username;

    }else{
        _userName = @"点击登录";
    }
    headView.userLabel.text = _userName;
    
    // 给表头添加手势
    headView.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAciton:)];
    [headView addGestureRecognizer:tap];

    
      return headView;
}

// 点击表头方法
- (void)tapAciton:(UITapGestureRecognizer *)tap
{
    BmobUser *user = [BmobUser getCurrentUser];
    if (user) {
        NSLog(@"已经登录");
        
          /*
        // 警告框
        UIAlertController *alertController =[UIAlertController alertControllerWithTitle:@"是否注销！" message:nil preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
        UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            NSLog(@"已注销");
            [BmobUser logout];
            [self.collectionView reloadData];
        }];
        [alertController addAction:okAction];
        [alertController addAction:cancelAction];
        [self presentViewController:alertController animated:YES completion:nil];
        */
        
        MyHomeController *myVc = [[MyHomeController alloc]init];
        [self presentViewController:myVc animated:YES completion:nil];
    }else{
        NSLog(@"还未登录");
        SHLoginView *loginVc = [[SHLoginView alloc]init];
        [self presentViewController:loginVc animated:YES completion:nil];
    }
         
}



#pragma mark - 表头大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    return CGSizeMake(KScreenWidth, KScreenHeight / 3);
}

#pragma mark - 点击cell触发方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
///////// 点击心愿单
    if (indexPath.item == 0 ) {
        
        BmobUser *user = [BmobUser getCurrentUser];
        
        // 判断是否登陆
        if (!user) {
            SHLoginView *loginVc = [[SHLoginView alloc]init];
            [self presentViewController:loginVc animated:YES completion:nil];
        }else{
            ListViewView *listVc = [[ListViewView alloc]init];
            [self presentViewController:listVc animated:YES completion:nil];
        }
    }
//////////点击设置
    else if (indexPath.item == 1)
    {
        SHSettingView *settingViewVc = [[SHSettingView alloc]init];
        [self presentViewController:settingViewVc animated:YES completion:nil];
    }
}



@end
